<html>
<head>
<title>自定义列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="../../build/common/base.css" rel="stylesheet">
<link href="../../build/form/form.css" rel="stylesheet">
<script type="text/javascript" src="../../build/common/base.js"></script>
<script type="text/javascript" src="../../build/common/gridList.js"></script>
<script type="text/javascript" src="../../build/common/ngEdit.js"></script>
<script type="text/javascript" src="../../assets/js/plugins/form/jquery.form.js"></script>
<script type="text/javascript" src="../../assets/js/common/tools/util.js"></script>
</head>
<body class="panel-body">
	<div class="panel panel-default search-panel">
		<div class="panel-heading">
			查询条件
			<a href="javascript:void(0)" class="pull-right">
				<i class="fa  fa-angle-double-up" toggle-collapse="#searchForm"></i>
			</a>
		</div>
		<div class="panel-body">
			<form class="form-inline panel-body pt0 pb0" id="searchForm">
				<div class="form-group mt5 bc-white lh34 h40  nowrap fn mr5 ml5">
					<label class="pt5 cell-nowarp overflow-eps fl pr10 lh24" title="名称">名称:</label>
					<span class="inline-block col-lg-8 col-xs-7 col-md-7 col-sm-7 ">
						<input type="text" class="form-control wd200" id="name_^VLK" />
					</span>
				</div>
				<div class="form-group mt5 bc-white lh34 h40  nowrap fn mr5 ml5">
					<label class="pt5 cell-nowarp overflow-eps fl pr10 lh24" title="编码">编码:</label>
					<span class="inline-block col-lg-8 col-xs-7 col-md-7 col-sm-7 ">
						<input type="text" class="form-control wd200" id="code_^VLK" />
					</span>
				</div>
				<div class="form-group mt5 bc-white lh34 h40  nowrap fn mr5 ml5">
					<label class="pt5 cell-nowarp overflow-eps fl pr10 lh24" title="数据源">数据源:</label>
					<span class="inline-block col-lg-8 col-xs-7 col-md-7 col-sm-7 ">
						<input type="text" class="form-control wd200" id="ds_key_^VLK" />
					</span>
				</div>
				<div class="form-group mt5 bc-white lh34 h40  nowrap fn mr5 ml5">
					<label class="pt5 cell-nowarp overflow-eps fl pr10 lh24" title="表名">表名:</label>
					<span class="inline-block col-lg-8 col-xs-7 col-md-7 col-sm-7 ">
						<input type="text" class="form-control wd200" id="table_name_^VLK" />
					</span>
				</div>
				<div class="form-group  mt5 bc-white lh34 h40  nowrap fn mr5">
					<button type="button" class="btn btn-primary btn-outline fa-search" ng-click="base.doSearch(this)">查询</button>
					<button type="button" class="btn btn-default btn-outline fa-rotate-left" ng-click="base.resetSearch(this)">重置</button>
				</div>
			</form>
		</div>
	</div>
	<div class="btn-group toolbar">
		<button class="btn btn-outline btn-primary fa-plus" openDialog="新增自定义列表" url="/form/formCustSql/formCustSqlEdit.html?operType=add" top="true">新增</button>
		<button class="btn btn-outline btn-primary fa-remove" onclick="mutiDel()">删除</button>
		<button class="btn btn-outline btn-primary fa-download" onclick="exportJson()">导出</button>
		<button class="btn btn-outline btn-primary fa-upload" onclick='$("#importModal").modal("show")'>导入</button>
		<button class="btn btn-outline btn-primary fa-plus" onclick='addToRes()'>添加到菜单</button>
	</div>
	<table ab-grid data-id-field="id" data-url="/form/formCustSql/listJson">
		<thead>
			<tr>
				<th data-checkbox="true" class="t-a-c">ID</th>
				<th data-field="name" data-sort='name' class="t-a-c">名称</th>
				<th data-field="code" data-sort='code' class="t-a-c">编码</th>
				<th data-field="dsKey" data-sort='dsKey' class="t-a-c">数据源</th>
				<th data-field="tableName" data-sort='tableName' class="t-a-c">表名</th>
				<th data-field="pkName" data-sort='pkName' class="t-a-c">主键</th>
				<th data-field="pageSize" data-sort='"pageSize"' class="t-a-c">默认分页</th>
				<th data-field="initQuery" data-sort='"initQuery"' class="t-a-c" data-formatter="labelFormatter" data-val-text="{'0':'否','1':'是'}">默认查询</th>
				<th data-field="canExport" data-sort='"canExport"' class="t-a-c" data-formatter="labelFormatter" data-val-text="{'0':'否','1':'是'}">导出</th>
				<th data-field="status" data-sort='status' class="t-a-c" data-formatter="labelFormatter" data-val-text="{'0':'禁用','1':'启用'}" data-val-style="{'0':'danger','1':'success'}">状态</th>
				<th data-field="createTime" data-sort='"createTime"' data-formatter="dateTimeFormatter" class="t-a-c">创建时间</th>
				<th data-field="creator" data-sort='"creator' class="t-a-c">创建人</th>
				<th data-field="id" data-formatter="innerHtmlFormatter" data-title="管理" class="t-a-c wd500">
					<div class="btn-group">
						<a class='btn btn-sm btn-outline  btn-success' openDialog='编辑{name}' qtip='编辑' top="true" url='/form/formCustSql/formCustSqlEdit.html?id={id}&operType=edit'>编辑</a>
						<a class='btn btn-sm btn-outline  btn-info ' openDialog='复制{name}' qtip='复制' top="true" url='/form/formCustSql/formCustSqlEdit.html?id={id}&operType=copy'>复制</a>
						<a class='btn btn-sm btn-outline  btn-primary ' openDialog='查看{name}' qtip='详情' top="true" url='/form/formCustSql/formCustSqlEdit.html?id={id}&operType=view'>详情</a>
						<a class='btn btn-sm btn-outline  btn-danger ' sendAction='删除' qtip='删除' url='/form/formCustSql/remove?id={id}'>删除</a>
						<a class='btn btn-sm btn-outline  btn-primary ' sendAction='启用' qtip='启用' url='/form/formCustSql/publish?id={id}' if="'{status}'=='0'">启用</a>
						<a class='btn btn-sm btn-outline  btn-warning ' sendAction='禁用' qtip='禁用' url='/form/formCustSql/disabled?id={id}' if="'{status}'=='1'">禁用</a>
						<a class='btn btn-sm btn-outline  btn-success ' qtip='预览' onclick="preview('{code}','{name}')">预览</a>
						<a class='btn btn-sm btn-outline  btn-info ' qtip='添加到菜单资源中' onclick="addToRes('{code}')">加入菜单</a>
					</div>
				</th>
			</tr>
		</thead>
	</table>
	<!-- 
	<a class='btn  btn-success fa fa-edit' openDialog='编辑{name}' qtip='编辑' top="true" url='/form/formCustSql/formCustSqlEdit.html?id={id}&operType=edit'></a>
						<a class='btn  btn-info fa fa-copy' openDialog='复制{name}' qtip='复制' top="true" url='/form/formCustSql/formCustSqlEdit.html?id={id}&operType=copy'></a>
						<a class='btn  btn-primary fa fa-detail' openDialog='查看{name}' qtip='详情' top="true" url='/form/formCustSql/formCustSqlEdit.html?id={id}&operType=view'></a>
						<a class='btn  btn-danger fa fa-trash' sendAction='删除' qtip='删除' url='/form/formCustSql/remove?id={id}'></a>
						<a class='btn  btn-primary fa fa-check' sendAction='启用' qtip='启用' url='/form/formCustSql/publish?id={id}' if="'{status}'=='0'"></a>
						<a class='btn  btn-warning fa fa-dot-circle-o' sendAction='禁用' qtip='禁用' url='/form/formCustSql/disabled?id={id}' if="'{status}'=='1'"></a>
						<a class='btn  btn-success fa fa-eye' qtip='预览' onclick="preview('{code}','{name}')"></a>
						<a class='btn  btn-info fa fa-plus' qtip='添加到菜单' onclick="addToRes('{code}')"> </a>
	
	 -->
	<div class="modal fade" id="importModal" role="dialog" data-backdrop="static" style="overflow-y: scroll;">
		<div class="modal-dialog wd1000" style="height: 800px;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title f16 text-center">导入自定义SQL</h4>
				</div>
				<div class="modal-body">
					<form id="importForm" name="importForm" method="post" enctype="multipart/form-data">
						<div class="form-group inline-block w100">
							<label class="col-sm-2 control-label">请选择json文件</label>
							<div class="col-sm-10 mt5">
								<input type="file" size="40" name="file" id="file" />
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer text-center">
					<button type="button" class="btn btn-default btn-outline" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-sm btn-primary btn-outline" onclick="importSave()">确认</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script>
	// 批量删除
    function mutiDel ( ) {

	    var ids = $.getDatagridCheckedId ( );
	    if ( !ids || ids.length == 0 ) {
		    $.Dialog.error ( "请至少选择一条数据~" );
		    return;
	    }

	    $.Dialog.confirm ( "温馨提示" , "确定要删除吗?" , function ( d ) {

		    $.ajax ( {
		        url : getCtxUrl ( 'form/formCustSql/remove?id=' + ids , true ) ,
		        // data : JSON.stringify(res),
		        type : 'POST' ,
		        contentType : 'application/json' ,
		        dataType : 'json' ,
		        async : false ,
		        success : function ( data ) {

			        window.reloadGrid && window.reloadGrid ( );
			        parent.reloadGrid && parent.reloadGrid ( );
			        if ( !data.isOk ) {
				        $.Dialog.error ( data.msg );
			        } else {
				        $.Dialog.success ( "删除成功" );
			        }
		        }
		    } );
	    } )

    }

    //导出
    function exportJson ( ) {

	    // 获取当前选中的字段
	    var ids = $.getDatagridCheckedId ( );
	    if ( !ids || ids.length == 0 ) {
		    $.Dialog.error ( "请至少选择一条数据~" );
		    return;
	    }
	    var url = __ctx + "/form/formCustSql/export?id=" + ids.join ( "," );
	    var form = $ ( '<form method="post" action="' + url + '" style="display:none;"></form>' );
	    document.body.appendChild ( form[0] ); // chrom 56 之后必须要将form加入到body中
	    form.submit ( );
	    form.remove ( );
    }
    function importSave ( ) {

	    var path = $ ( '#file' ).val ( );
	    var extNaem = path.substring ( path.length - 4 , path.length );
	    if ( extNaem != 'json' ) {
		    $.Dialog.error ( "请选择 *.json文件进行导入!" );
		    return;
	    }
	    var url = __ctx + "/form/formCustSql/import";
	    $ ( "#importForm" ).ajaxForm ( {
	        type : "post" ,
	        url : url ,
	        contentType : "application/json;charset=UTF-8" ,
	        success : function ( data ) {

		        data = JSON.parse ( data );
		        window.reloadGrid && window.reloadGrid ( );
		        parent.reloadGrid && parent.reloadGrid ( );
		        if ( !data.isOk ) {
			        $.Dialog.error ( data.msg );
		        } else {
			        $ ( "#importModal" ).modal ( "hide" )
			        $.Dialog.success ( "导入成功" );
		        }
	        }
	    } );

	    $ ( "#importForm" ).submit ( );
    }

    function preview ( code , name ) {

	    var url = "form/formCustSql/view/formCustSqlView.html?code=" + code;
	    if ( top && top.addTab ) {
		    top.addTab ( {
		        id : code ,
		        name : name ,
		        icon : "fa-list" ,
		        url : url ,
		        closable : true
		    } );
	    } else {
		    window.open ( "view/formCustSqlView.html?code=" + code );
	    }
    }
    function addToRes ( code ) {

	    var codeList = [ ];
	    if ( !code ) {
		    var arrays = $ ( "[ab-grid]" ).bootstrapTable ( 'getSelections' );
		    if ( !arrays || arrays.length == 0 ) {
			    $.Dialog.error ( "请至少选择一条数据" );
			    return;
		    }
		    for ( var i = 0 ; i < arrays.length ; i++ ) {
			    codeList.push ( arrays[i].code );
		    }
	    } else {
		    codeList = [ code ];
	    }
	    
	    CustUtil.openCustDialog ( "resDialog" , function ( data , innerWindow ) {

		    var hasError = false;
		    for ( var i = 0 ; i < codeList.length ; i++ ) {
			    hasError = saveRes ( codeList[i] , data );
		    }

		    if ( !hasError ) {
			    $.Dialog.success ( "添加成功" );
			    $.Dialog.close ( innerWindow );
		    }
	    } )
    }

    function saveRes ( code , data ) {

	    var buttons = [ ];
	    var name = "";
	    // 获取配置
	    $.ajax ( {
	        url : getCtxUrl ( '/form/formCustSql/view/vo_' + code , true ) ,
	        // data : JSON.stringify(res),
	        type : 'POST' ,
	        contentType : 'application/json' ,
	        dataType : 'json' ,
	        async : false ,
	        success : function ( result ) {

		        buttons = JsonUtil.ps ( result.data.buttons );
		        name = result.data.name;
	        }
	    } );
	    var hasError = false;
	    var systemId = data[0].systemId;
	    var res = {
	        name : name ,
	        alias : "customsql_" + code ,
	        type : "menu" ,
	        enable : "1" ,
	        opened : "1" ,
	        sn : "1" ,
	        icon : "fa-list" ,
	        parentId : data[0].id ,
	        systemId : systemId ,
	        url : "form/formCustSql/view/formCustSqlView.html?code=" + code ,
	    }
	    var saveUrl = __ctx + "/org/sysResource/save";
	    $.ajax ( {
	        url : saveUrl ,
	        data : JSON.stringify ( res ) ,
	        type : 'POST' ,
	        contentType : 'application/json' ,
	        dataType : 'json' ,
	        async : false ,
	        success : function ( result ) {

		        if ( result.isOk ) {//成功
			        // 添加按钮
			        var resList = [ ];

			        var sn = 0;
			        res = {
			            name : "查询" ,
			            alias : "customsql_list_" + code ,
			            type : "button" ,
			            enable : "1" ,
			            opened : "1" ,
			            sn : ++sn ,
			            parentId : result.data ,
			            systemId : systemId ,
			            icon : "fa-search" ,
			            url : "form/formCustSql/view/list_" + code ,
			        }
			        resList.push ( res );

			        res = {
			            name : "导出" ,
			            alias : "customsql_export_" + code ,
			            type : "button" ,
			            enable : "1" ,
			            opened : "1" ,
			            sn : ++sn ,
			            parentId : result.data ,
			            systemId : systemId ,
			            icon : "fa-download" ,
			            url : "form/formCustSql/view/export_" + code ,
			        }
			        resList.push ( res );

			        // 自定义按钮
			        for ( var i = 0 ; i < buttons.length ; i++ ) {
				        res = {
				            name : buttons[i].name ,
				            alias : "customsql_button_" + code + "_" + buttons[i].alias ,
				            type : "button" ,
				            enable : "1" ,
				            opened : "1" ,
				            sn : ++sn ,
				            parentId : result.data ,
				            systemId : systemId ,
				            icon : buttons[i].icon ,
				            url : buttons[i].url || "form/formCustSql/view/changeData_" + code + "_" + buttons[i].alias ,
				        }
				        resList.push ( res );
			        }
			        // 保存按钮
			        for ( var j = 0 ; j < resList.length ; j++ ) {
				        $.ajax ( {
				            url : saveUrl ,
				            data : JSON.stringify ( resList[j] ) ,
				            type : 'POST' ,
				            contentType : 'application/json' ,
				            dataType : 'json' ,
				            async : false ,
				            success : function ( result ) {

				            }
				        } )
			        }
		        } else {
			        hasError = true;
			        $.Dialog.alert ( result.msg , 2 );
		        }
	        }
	    } )

	    return hasError;
    }
</script>
</html>